package leetcode;

import java.util.Arrays;
import java.util.Comparator;

public class Leetcode435 {
    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals == null || intervals.length == 0) {
            return 0;
        }
        int n = intervals.length;
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        int total = 0, prev = intervals[0][1];
        for (int i = 1; i < n; ++i) {
            if (intervals[i][0] < prev) {
                ++total;
            } else {
                prev = intervals[i][1];
            }
        }
        return total;
    }

    public static void main(String[] args) {
        int[][] intervals = {{1,2},{2,4},{1,3}};
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        for (int m=0 ; m< intervals.length ; m++) {
            for (int n=0 ; n< intervals.length ; n++) {
                System.out.print(intervals[m][n]);
                System.out.print("|");
            }
            System.out.println();
        }
    }
}
